<!--
 * @Autor: lh
 * @Date: 2023-08-21 09:50:00
 * @LastEditors: lh
 * @LastEditTime: 2024-03-04 17:32:44
 * @Description: 
-->
<script lang="ts" setup>
import { useUserStore } from "@/store/modules/user"
// import Admin from "./components/Admin.vue"
// import Editor from "./components/Editor.vue"
import { useUserStoreHook } from "@/store/modules/user"
import { selectProcessMatterCountVo, selectAll } from "@/api/common"
import { reactive, ref } from "vue"
import NotifyList from "@/components/Notify/NotifyList.vue"
import { useRouter } from "vue-router"
import type { FormInstance, FormRules } from "element-plus"
import upfilebox from "@/components/table/upfile.vue"
import { changeUserInfo } from "@/api/common"
import { ElMessage } from "element-plus"

const userStore = useUserStore()
const userInfo = useUserStoreHook()

const router = useRouter()

//#region  用户信息 模块

interface RuleForm {
  name: string
  image: string
  userName: string
  password: string
  gender: number
}

const formSize = ref("default")
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: "",
  image: "",
  userName: "",
  password: "",
  gender: 1
})

ruleForm.name = userInfo.username
ruleForm.image = userInfo.headImg
ruleForm.userName = userInfo.account
ruleForm.gender = userInfo.gender

console.log("userInfo---", userInfo)

const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  image: [
    {
      required: true,
      message: "请上传头像",
      trigger: "change"
    }
  ],
  userName: [
    {
      required: true,
      message: "登录账号不能为空",
      trigger: "blur"
    }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log("submit!")
      changeUserInfo(ruleForm)
        .then((res) => {
          let userinfos = JSON.parse(localStorage.getItem("userinfo") || "{}")
          userinfos = {
            ...userinfos,
            ...ruleForm
          }
          localStorage.setItem("userinfo", JSON.stringify(userinfos))
          userInfo.getInfo()
          ElMessage({
            message: "修改成功~",
            type: "success"
          })
        })
        .catch(() => {})
    } else {
      console.log("error submit!", fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`
}))

//#endregion 用户信息模块end

// const isAdmin = userStore.roles.includes("admin")

const processMatter = reactive({
  completedCount: 0, // 已办个数
  toDoCount: 0 // 待办个数
})
// 获取个数
selectProcessMatterCountVo({})
  .then((res) => {
    if (res.code == 200) {
      let data = res.data
      processMatter.completedCount = data.completedCount
      processMatter.toDoCount = data.toDoCount
    }
  })
  .catch(() => {})

let processMatterList = ref([])

// 获取状态
selectAll({
  page: 1,
  pageSize: 4,
  processState: 1
})
  .then((res) => {
    if (res.code == 200) {
      let data = res.data
      processMatterList.value = data.records.map((it) => {
        // it.til = it.
        return it
      })
    }
  })
  .catch(() => {})

const handleHistory = () => {
  router.push({
    path: "/todolist"
  })
}
</script>

<template>
  <div class="homeboxs">
    <!-- <component :is="isAdmin ? Admin : Editor" /> -->
    <div class="homeboxs_box">您好! 尊敬的{{ userInfo.username }}~欢迎来到CNCC</div>
    <div class="homeboxs_body">
      <el-card class="box-user" v-if="false">
        <template #header>
          <div class="card-header">
            <span>用户信息</span>
          </div>
        </template>
        <div class="">
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            :size="formSize"
            status-icon
          >
            <el-form-item label="用户名" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item label="头像" prop="image">
              <!-- <el-input v-model="ruleForm.name" /> -->
              <upfilebox fileType="img" v-model="ruleForm.image"></upfilebox>
            </el-form-item>
            <el-form-item label="登录账号" prop="userName">
              <el-input v-model="ruleForm.userName" />
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="ruleForm.password" placeholder="登录密码" />
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="ruleForm.gender">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
                <el-radio :label="3">未知</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleFormRef)">确认</el-button>
              <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>任务</span>
            <div style="margin-top: 10px; display: inline-block; margin-left: 10px">
              <el-badge :value="processMatter.toDoCount || 0" :max="99" class="item">
                <el-button>待办任务</el-button>
              </el-badge>
              <!-- <el-badge :value="processMatter.completedCount || 0" :max="99" class="item">
                <el-button style="margin-left: 30px">已办任务</el-button>
              </el-badge> -->
            </div>
          </div>
        </template>
        <NotifyList :list="processMatterList"></NotifyList>
        <div class="notify-history" v-if="processMatterList.length > 0">
          <el-button link @click="handleHistory">查看更多待办任务</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.homeboxs {
  // background: #fff;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  &_box {
    margin-bottom: 20px;
  }
  &_body {
    display: flex;
    justify-content: space-between;
    .box-user {
      width: 300px;
      margin-right: 20px;
    }
    .box-card {
      flex: 1;
    }
  }
}
.notify-history {
  text-align: center;
  padding-top: 12px;
  border-top: 1px solid var(--el-border-color);
}
</style>
